﻿using System;
using System.Security.Cryptography;

namespace HoverTree.HoverTreeFrame
{
    public  class HoverString
    {
        #region 获取后几位数 public string GetLastStr(string str,int num)
        /// <summary>
        /// 获取后几位数
        /// </summary>
        /// <param name="str">要截取的字符串</param>
        /// <param name="num">返回的具体位数</param>
        /// <returns>返回结果的字符串</returns>
        public static string GetLastStr(string str, int num)
        {
            int count = 0;
            if (str.Length > num)
            {
                count = str.Length - num;
                str = str.Substring(count, num);
            }
            return str;
        }
        #endregion

        /// <summary>
        /// MD5加密字符串(30位)
        /// </summary>
        /// <param name="myString"></param>
        /// <returns></returns>
        public static string GetKeleyiMD5(string myString)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
            byte[] targetData = md5.ComputeHash(fromData);
            string byte2String = null;

            for (int i = 0; i < targetData.Length; i++)
            {
                byte2String += targetData[i].ToString("x");
            }

            return byte2String;
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input"></param>
        /// <param name="format">1表示32位大写，2表示32位小写，3表示16位大写，4表示16位小写</param>
        /// <returns></returns>
        public static string GetMd5(string input, int format)
        {
            switch (format)
            {
                case 1:
                    return GetMd5Upper32(input);
                case 2:
                    return GetMd5Upper32(input).ToLower();
                case 3:
                    return GetMd5Upper32(input).Substring(8, 16);
                case 4:
                    return GetMd5Upper32(input).Substring(8, 16).ToLower();
                default:
                    return GetMd5Upper32(input);
            }
        }

        static string GetMd5Upper32(string input)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(input, "MD5");
        }

        public static string GetYear(DateTime dateTime)
        {
            return dateTime.Year.ToString();
        }
    }
}